package com.yanqu.road.dao.impl.activity.share;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.OpenInvitedData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OpenInvitedDataDaoImpl extends TempDataObjectDao<OpenInvitedData> {

    public static String getOpenInvitedDataSql = "select * from `t_s_open_invited_data` where `invitedCode` != ''";

    public Map<String, OpenInvitedData> getOpenInvitedDataMap() {
        Map<String, OpenInvitedData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getOpenInvitedDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                OpenInvitedData data = parse(rs);
                map.put(data.getOpenId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateOpenInvitedDataSql = "update `t_s_open_invited_data` set invitedCode=?,registerTime=? where openId=? and registerTime>?";

    @Override
    protected boolean updateData(OpenInvitedData openInvitedData) {
        openInvitedData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, openInvitedData.getInvitedCode()));
        para.put(i++, new DbParameter(Types.BIGINT, openInvitedData.getRegisterTime()));
        // where
        para.put(i++, new DbParameter(Types.VARCHAR, openInvitedData.getOpenId()));
        para.put(i++, new DbParameter(Types.BIGINT, openInvitedData.getRegisterTime()));
        if (getDb().executeNoneQuery(updateOpenInvitedDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertOpenInvitedDataSql = "insert ignore into `t_s_open_invited_data`(openId,invitedCode,registerTime) values (?,?,?)";

    @Override
    protected boolean addData(OpenInvitedData openInvitedData) {
        openInvitedData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, openInvitedData.getOpenId()));
        para.put(i++, new DbParameter(Types.VARCHAR, openInvitedData.getInvitedCode()));
        para.put(i++, new DbParameter(Types.BIGINT, openInvitedData.getRegisterTime()));
        if (getDb().executeNoneQuery(insertOpenInvitedDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected OpenInvitedData parseData(ResultSet rs) throws SQLException {
        OpenInvitedData openInvitedData = new OpenInvitedData();
        openInvitedData.setOpenId(rs.getString("openId"));
        openInvitedData.setInvitedCode(rs.getString("invitedCode"));
        openInvitedData.setRegisterTime(rs.getLong("registerTime"));
        return openInvitedData;
    }
}
